import Dayjs from "dayjs";

export const mouthMap = {
  Jan: "1月",
  Feb: "2月",
  Mar: "3月",
  Apr: "4月",
  May: "5月",
  Jun: "6月",
  Jul: "7月",
  Aug: "8月",
  Sep: "9月",
  Oct: "10月",
  Nov: "11月",
  Dec: "12月"
};

export function getDayWeek(date) {
  var l = ["日", "一", "二", "三", "四", "五", "六"];
  var d = new Date(date).getDay();
  return "周" + l[d];
}
export function getGanttConfigByZoomValue(gantt, value) {
  const yearFormat = (date) => {
    return `${Dayjs(date).format("YYYY") + "年"}`;
  };
  if (value === "week") {
    const dayFormat = function (date) {
      const weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
      return Dayjs(date).format("MM-DD") + " " + weeks[Dayjs(date).day()];
    };

    return [
      { unit: "year", step: 1, format: yearFormat },
      { unit: "day", step: 1, format: dayFormat }
    ];
  } else if (value === "month") {
    const monthFormat = (date) => {
      return Dayjs(date).month() + 1 + "月" + Dayjs(date).date() + "号";
    };
    return [
      { unit: "year", step: 1, format: yearFormat },
      { unit: "day", step: 3, format: monthFormat }
    ];
  } else if (value === "quarter") {
    const FormatQuarter = (date) => {
      console.log(Dayjs(date).month() + 1 + "月");
      return Dayjs(date).year() + "年" + (Dayjs(date).month() + 1) + "月";
    };
    const dayFormat = (date) => {
      const endDate = gantt.date.add(
        gantt.date.add(date, 1, "week"),
        -1,
        "day"
      );
      // 处理一下月份
      return Dayjs(date).date() + "号" + "-" + Dayjs(endDate).date() + "号";
    };
    return [
      { unit: "month", step: 1, format: FormatQuarter },
      { unit: "week", step: 1, format: dayFormat }
    ];
  } else if (value === "year") {
    const monthFormat = (date) => {
      return Dayjs(date).month() + 1 + "月";
    };
    return [
      { unit: "year", step: 1, format: yearFormat },
      { unit: "month", step: 1, format: monthFormat }
    ];
  }
}

const editMapType = {
  textEditor: { type: "text", map_to: "text" },
  dateEditor: {
    type: "date",
    map_to: "start_date",
    min: new Date(2018, 0, 1),
    max: new Date(2019, 0, 1)
  },
  durationEditor: { type: "number", map_to: "duration", min: 0, max: 100 }
};
export const getEditType = (target) => {
  if (!target || Object.keys(target).length === 0 || !target.editor) {
    return {};
  }
  return editMapType[target.editor] || {};
};

export const hasKey = (target, key) => {
  return Object.prototype.hasOwnProperty.call(target, key);
};
export const getStartDate = (source) => {
  if (hasKey(source, "start_date")) {
    if (isDate(source.start_date)) {
      return source.start_date;
    } else {
      return new Date(source.start_date);
    }
  }
  return "";
};

export const createCheckBoxTemplate = (gantt, item) => {
  const children = gantt.getChildren(item.id);
  if (children.length === 0) {
    if (item.checked) {
      return `<input type="checkbox" class="checkboxCls" id=${
        item ? item.id : "all"
      } checked /> <span style="font-size:14px;color:#1C1C1E">${
        item.text
      }</span>`;
    } else {
      return `<input type="checkbox" class="checkboxCls" id=${
        item ? item.id : "all"
      } /> <span style="font-size:14px;color:#1C1C1E">${item.text}</span>`;
    }
  } else {
    return `${item.text}`;
  }
};

export const today = () => {
  return Dayjs(new Date());
};

export const isArray = (array) => {
  return Array.isArray(array);
};

export const isObject = (val) => {
  return toString.call(val) === "[object Object]";
};

export function isDate(val) {
  return toString.call(val) === "[object Date]";
}
